# -*- coding: utf-8 -*-
from datetime import datetime
from django.db import models
from django.core.urlresolvers import reverse
from Colaboradores.models import Colaborador
from Scope.thumbs import ImageWithThumbsField
from Scope.models import BaseNoticia
def content_image_name(instance, filename):
    return '/'.join(['arquivos/galeria', str(instance.sluggaleria), filename])
def imagem_galeria(instance, filename):
    return '/'.join(['arquivos/galeria', str(instance.galeriaImagem.sluggaleria), filename])

class Galeria(BaseNoticia):
    subtitulo = models.CharField(max_length=200)
    chamada = models.CharField(max_length=30)
    fonte = models.CharField(max_length=20, blank=True, null=True)
    imagem = ImageWithThumbsField('Imagem Destaque', upload_to=content_image_name, sizes=((800,600),(300,225)))
    sluggaleria = models.SlugField(max_length=200, blank=True)
    def __unicode__(self):
        return self.titulo
    def get_absolute_url(self):
        return reverse('Scope.views.galeria', kwargs={'sluggaleria': self.sluggaleria})

class Imagem(models.Model):
    foto = ImageWithThumbsField('Imagem Destaque', upload_to=imagem_galeria, sizes=((918,573),(170,127)))
    galeriaImagem = models.ForeignKey(Galeria, related_name='galeriaImagem')
    class Meta:
        verbose_name = 'Imagem'
        verbose_name_plural = 'Imagens'

# SIGNALS
from django.db.models import signals
from django.template.defaultfilters import slugify

def galeria_pre_save(signal, instance, sender, **kwargs):
    instance.sluggaleria = slugify(instance.titulo)

signals.pre_save.connect(galeria_pre_save, sender=Galeria)